<template>
  <div class="fade-carousel">
    <transition-group name="fade" tag="div" class="fade-track">
      <div class="fade-item" v-for="(item, idx) in [items[current]]" :key="current">
        <img v-if="item.img" :src="item.img" :alt="item.title || ''" />
        <div v-else class="fade-content">
          <slot :item="item">{{ item.title }}</slot>
        </div>
      </div>
    </transition-group>
    <el-button class="nav-btn left" @click="prev" circle>
      <el-icon>
        <ArrowLeft />
      </el-icon>
    </el-button>
    <el-button class="nav-btn right" @click="next" circle>
      <el-icon>
        <ArrowRight />
      </el-icon>
    </el-button>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { ArrowLeft, ArrowRight } from '@element-plus/icons-vue'

const props = defineProps({
  items: { type: Array, required: true },
})

const current = ref(0)
const len = props.items.length

const prev = () => {
  current.value = (current.value - 1 + len) % len
}
const next = () => {
  current.value = (current.value + 1) % len
}
</script>

<style lang="scss" scoped>
.fade-carousel {
  position: relative;
  width: 600px;
  height: 320px;
  margin: 0 auto;

  .fade-track {
    width: 100%;
    height: 320px;
    position: relative;
    overflow: hidden;
  }

  .fade-item {
    position: absolute;
    width: 100%;
    height: 100%;
    left: 0;
    top: 0;
    display: flex;
    align-items: center;
    justify-content: center;

    img {
      width: 100%;
      height: 100%;
      object-fit: cover;
      border-radius: 18px;
    }

    .fade-content {
      font-size: 2rem;
      color: #6366f1;
      text-align: center;
    }
  }

  .fade-enter-active,
  .fade-leave-active {
    transition: opacity 0.7s;
  }

  .fade-enter-from,
  .fade-leave-to {
    opacity: 0;
  }

  .nav-btn {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    z-index: 10;

    &.left {
      left: 12px;
    }

    &.right {
      right: 12px;
    }
  }
}
</style>